//计时器工具类

//显示时间信息；
export function showTime() {
    let timeDate = new Date();
    let year = timeDate.getFullYear();
    let month = timeDate.getMonth() + 1;
    let date = timeDate.getDate();
    let day = timeDate.getDay();
    let hour = timeDate.getHours();
    let minute = timeDate.getMinutes();
    let second = timeDate.getSeconds();
    month = checkFigure(month);
    date = checkFigure(date);
    hour = checkFigure(hour);
    minute = checkFigure(minute);
    second = checkFigure(second);
    let weekday = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');
    return `${year}年${month}月${date}日 ${weekday[day]} ${hour}:${minute}:${second}`;
}

/**
 * 转换格式
 * @param {Date} date 
 */
export function getFormateTime(date) {
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let date2 = date.getDate();
    month = checkFigure(month);
    date2 = checkFigure(date2);
    return `${year}-${month}-${date2}`;
}


/**
 * 根据日期对象获取常见的时间格式
 * @param {Date} timeDate 
 */
export function getFormatTimeByDateObj(timeDate) {
    if (!isValidDate(timeDate)) {
        return '';
    }
    let year = timeDate.getFullYear();
    let month = timeDate.getMonth() + 1;
    let date = timeDate.getDate();
    let hour = timeDate.getHours();
    let minute = timeDate.getMinutes();
    let second = timeDate.getSeconds();
    minute = checkFigure(minute);
    second = checkFigure(second);
    return `${year}-${month}-${date} ${hour}:${minute}:${second}`;
}

//获取时间搓转换为常见时间格式
export function getFormTimeByTimestamp(timestamp) {
    let date = new Date(timestamp);
    return getFormateTime(date);
}

//把单一位数字通过在前面+“0”转化为两位；
function checkFigure(i) {
    if (i < 10) {
        i = '0' + i;
    }
    return i;
}

//判断是不是可用的时间
function isValidDate(date) {
    return date instanceof Date && !isNaN(date.getTime())
}


//根据年获取年第一天
function getTimeByYear(y) {
    return `${y}-01-01`;
}

export default {
    getFormateTime,
    showTime,
    getFormatTimeByDateObj,
    getFormTimeByTimestamp,
    getTimeByYear
}